﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using RoadTeam.Enums;
namespace DataBaseUpdate
{
   public class SqlHelper
    {
        /// <summary>
        /// 重载，返回执行结果信息
        /// </summary>
        /// <param name="cmdText"></param>
        /// <param name="ct"></param>
        /// <returns></returns>
        public static string ExcuteQueryReturnMessage(string cmdText, CommandType ct)
        {
            try
            {
                ExecuteNonQuery(cmdText, ct);
                return "执行成功！";
            }
            catch (Exception e)
            {
                return e.Message;
            }
        }

       public static int ExecuteNonQuery(string cmdText, CommandType ct)
        {
            int res;
            SqlCommand cmd = null;
            using (cmd = new SqlCommand(cmdText, GetConn()))
            {
                cmd.CommandType = ct;
                res = cmd.ExecuteNonQuery();
            }
            return res;
        }

        /// <summary>
        ///  打开Connection对象
        /// </summary>
        private static SqlConnection GetConn()
        {
            SqlConnection conn = new SqlConnection(GlobalConfig.ConnectionString);
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }
            return conn;
        }
    }
}
